﻿Imports NewHeaderSystems.Archive.Engine
Imports NewHeaderSystems.Archive.Data

Public Class ArchiveHistory
    Inherits PageTemplate

    Private Sub ArchiveHistory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Title = "Archive Maintenance"
        Description = "View all recent archive activity; rollback (remove) archived confirms from the database."
        PageIcon = My.Resources.Folder_Secure

        LoadYears()
        LoadMonths()
        ResetToCurrentDate()
    End Sub

    Private Sub LoadYears()
        Dim years As New Dictionary(Of Integer, String)

        Dim year = Date.Now.Year - 10
        For index = 0 To 19
            cboArchivedYear.Items.Add(year + index)
        Next

    End Sub

    Private Sub LoadMonths()

        For index = 1 To 12
            cboArchivedMonth.Items.Add(MonthName(index))
        Next
    End Sub

    Private Sub ResetToCurrentDate()
        cboArchivedYear.SelectedIndex = 10
        cboArchivedMonth.SelectedIndex = Date.Now.Month - 1
    End Sub

    ''' <summary>
    ''' Load the grid filtered by the specified month
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadHistory()
        If cboArchivedYear.SelectedIndex >= 0 And cboArchivedMonth.SelectedIndex >= 0 Then
            LoadHistory(New Date(Integer.Parse(cboArchivedYear.Text), cboArchivedMonth.SelectedIndex + 1, 1))
        End If
    End Sub
    Private Sub LoadHistory(ByVal month As Date)
        Me.ParentForm.Cursor = Cursors.WaitCursor

        Dim lastArchived As Date = DataScript.GetLastArchived()
        Dim archiveHistoryView As List(Of Core.ArchiveHistoryView) = DataScript.GetArchiveHistory(month)

        Dim bs As New BindingSource
        bs.DataSource = archiveHistoryView
        archiveHistoryGrid.DataSource = bs
        For Each row As DataGridViewRow In archiveHistoryGrid.Rows
            Dim historyItem As Core.ArchiveHistoryView = DirectCast(row.DataBoundItem, Core.ArchiveHistoryView)
            If DateDiff(DateInterval.Day, historyItem.DateArchived, lastArchived) = 0 Then 'Only allow delete for the last day
                row.Cells("DeleteLink").Value = "Delete"
            End If
        Next
        Me.ParentForm.Cursor = Cursors.Default
    End Sub

    Private Sub ArchiveHistory_PageEnter(ByVal sender As System.Object, ByVal e As Microsoft.Samples.Windows.Forms.Navigation.PageEventArgs) Handles MyBase.PageEnter
        LoadHistory()
    End Sub

    Private Sub btnView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        LoadHistory()
    End Sub

    Private Sub archiveHistoryGrid_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles archiveHistoryGrid.CellContentClick
        If e.ColumnIndex = archiveHistoryGrid.Columns("DeleteLink").Index Then
            DeleteArchive(e.RowIndex)
        End If
    End Sub

    Private Sub DeleteArchive(ByVal rowIndex As Integer)
        Dim archiveHistoryID As Integer
        If MessageBox.Show("Delete the confirms that were archived by this file?", "Delete Confirms", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning) = DialogResult.Yes Then
            archiveHistoryID = CInt(archiveHistoryGrid.Item("ID", rowIndex).Value)
            DataScript.DeleteArchiveHistory(archiveHistoryID, App.Instance.UserID)
            LoadHistory()
        End If
    End Sub

    Private Sub btnDeleteConfirms_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteConfirms.Click
        If MessageBox.Show("Permanently delete all confirms? You cannot undo this action.", "Delete Confirms", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation) = DialogResult.Yes Then
            DataScript.ClearConfirms()
            GoToPage(FormTypes.Administration)
        End If
    End Sub

    Private Sub cboArchivedYear_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboArchivedYear.SelectedIndexChanged
        LoadHistory()
    End Sub

    Private Sub cboArchivedMonth_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboArchivedMonth.SelectedIndexChanged
        LoadHistory()
    End Sub
End Class
